package com.molyfun.parents.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class HorizonalThreeTabBar extends LinearLayout implements View.OnClickListener{

    private int mChooseTab = ThreeTabBar.ThreeTabChooseTab.BAR_ONE;

    protected LayoutInflater layoutInflater;

    private ThreeTabBar.ThreeBarClickListener listener;
    private Context context;
    private boolean inited;

    @InjectView(R.id.tab_one)
    TextView mTabOne;
    @InjectView(R.id.tab_two)
    TextView mTabTwo;
    @InjectView(R.id.tab_three)
    TextView mTabThree;

    public HorizonalThreeTabBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public HorizonalThreeTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public HorizonalThreeTabBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_horizonal_three_tab_choose_bar, this);
        ButterKnife.inject(this);
        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabThree.setOnClickListener(this);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

    /**
     * init view
     *
     */
    public void initData(String tab1, String tab2, String tab3) {
        if(inited){
            return;
        }


        mTabOne.setText(tab1);
        mTabTwo.setText(tab2);
        mTabThree.setText(tab3);
        inited = true;
    }

//    private String getTabTextShow(String tabText){
//        if(!TextUtils.isEmpty(tabText)){
//            if(tabText.length() <= 3){
//                return tabText;
//            }else if(tabText.length() == 4){
//                return tabText.substring(0, 2) + "\n" + tabText.substring(2);
//            }else if(tabText.length() >= 5){
//                return tabText.substring(0, 3) + "\n" + tabText.substring(3);
//            }
//        }
//        return "";
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tab_one:
                if(mChooseTab != ThreeTabBar.ThreeTabChooseTab.BAR_ONE){
                    setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_ONE);
                    if(listener != null){
                        listener.onThreeBarClick(ThreeTabBar.ThreeTabChooseTab.BAR_ONE);
                    }
                }
                break;
            case R.id.tab_two:
                if(mChooseTab != ThreeTabBar.ThreeTabChooseTab.BAR_TWO){
                    setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_TWO);
                    if(listener != null){
                        listener.onThreeBarClick(ThreeTabBar.ThreeTabChooseTab.BAR_TWO);
                    }
                }
                break;
            case R.id.tab_three:
                if(mChooseTab != ThreeTabBar.ThreeTabChooseTab.BAR_THREE){
                    setTabClick(ThreeTabBar.ThreeTabChooseTab.BAR_THREE);
                    if(listener != null){
                        listener.onThreeBarClick(ThreeTabBar.ThreeTabChooseTab.BAR_THREE);
                    }
                }
                break;
        }
    }

    /**
     * only view
     * @param tab
     */
    public void setTabClick(int tab){
        if(tab == ThreeTabBar.ThreeTabChooseTab.BAR_ONE){
            mTabOne.setBackgroundResource(R.drawable.bg_left_rounded_red);
            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mTabTwo.setBackgroundColor(ContextCompat.getColor(context, R.color.common_tab_choose_bar_normal_bg));
            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.text_hint_color));
            mTabThree.setBackgroundResource(R.drawable.bg_right_rounded_red_hollow);
            mTabThree.setTextColor(ContextCompat.getColor(context, R.color.text_hint_color));
            mChooseTab = ThreeTabBar.ThreeTabChooseTab.BAR_ONE;
        }else if(tab == ThreeTabBar.ThreeTabChooseTab.BAR_TWO){
            mTabOne.setBackgroundResource(R.drawable.bg_left_rounded_red_hollow);
            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.text_hint_color));
            mTabTwo.setBackgroundColor(ContextCompat.getColor(context, R.color.color_base));
            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mTabThree.setBackgroundResource(R.drawable.bg_right_rounded_red_hollow);
            mTabThree.setTextColor(ContextCompat.getColor(context, R.color.text_hint_color));
            mChooseTab = ThreeTabBar.ThreeTabChooseTab.BAR_TWO;
        }else{
            mTabOne.setBackgroundResource(R.drawable.bg_left_rounded_red_hollow);
            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.text_hint_color));
            mTabTwo.setBackgroundColor(ContextCompat.getColor(context, R.color.common_tab_choose_bar_normal_bg));
            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.text_hint_color));
            mTabThree.setBackgroundResource(R.drawable.bg_right_rounded_red);
            mTabThree.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mChooseTab = ThreeTabBar.ThreeTabChooseTab.BAR_THREE;
        }
    }

    public void setThreeBarClickListener(ThreeTabBar.ThreeBarClickListener listener) {
        this.listener = listener;
    }


}